
package com.eascs.web.o2o.scheduler.test;

import javax.inject.Inject;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.service.IArchiveService;
import com.eascs.admin.o2o.badloan.service.ICancelService;
import com.eascs.admin.o2o.loanAccount.service.Io2oLoanapplyAccountService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.o2o.scheduler.dao.MonitorDao;
import com.eascs.web.o2o.scheduler.service.CrmByServiceService;
import com.eascs.web.o2o.scheduler.service.PersonalAccountService;
import com.eascs.web.o2o.scheduler.service.SynchroAfterLoanForService;
import com.eascs.web.o2o.scheduler.service.SynchroService;
import com.eascs.web.o2o.scheduler.service.SynoInfoAssetStructureBjService;
import com.eascs.web.o2o.scheduler.service.SynoInfoAssetStructureService;
import com.eascs.web.o2o.scheduler.task.AdminSynchroTask;

@RestController
public class AdminController extends BaseRestController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	
	@Inject
	SynchroService synchroService;
	
	@Inject
	AdminSynchroTask adminSynchroTask;
	
	@Inject
	SynchroAfterLoanForService synchroAfterLoanForService;

	@Reference
	IArchiveService iArchiveService;
	
	@Reference
	ICancelService iCancelService;
	
	@Inject
	MonitorDao  monitorDao;
	
	@Inject
	SynoInfoAssetStructureService synoInfoAssetStructureService;
	
	@Inject
	CrmByServiceService crmByServiceService;
	
	@Inject
	PersonalAccountService  personalAccountService;
	@Reference
	Io2oLoanapplyAccountService o2oLoanapplyAccountService;

	@RequestMapping(value = "/o2o/admin/creditManager")
	public void creditManager() {
		StopWatch s1=new StopWatch();
		logger.info("service信贷经理数据同步数据开始");
		adminSynchroTask.creditManager();
		logger.info("service信贷经理数据同步数据结束，耗时：[{}]秒",s1.elapsedTime()/1000);
	}
	
	@Inject
	SynoInfoAssetStructureBjService synoInfoAssetStructureBjService;
	
	@RequestMapping(value = "/o2o/scheduler/workflow/creditArchive")
	public void creditArchive() {
		logger.info("[workflow] [归档0点]任务[开始]......");
		StopWatch s =new StopWatch();
		try {
			com.eascs.admin.common.vo.ModelResult<Boolean> result = iArchiveService.buildArchiveExport();
			logger.info("接口返回对象：[{}]",result.getModel());
		} catch (Exception e) {
			e.getMessage();
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[workflow] [归档0点]任务[结束]......耗时：【{}】分", s.elapsedTime() / 1000 / 60);
	}
	
	
	
	/**
	 * 同步service 数据字典（手动触发一次）
	 * 
	 * 2016年6月14日
	 */
	@RequestMapping(value = "/o2o/scheduler/service/datadic/synch")
	public void serviceDataDic() {
		StopWatch s1=new StopWatch();
		logger.info("service同步数据字典开始");
			synchroService.savePubDatadictionary();
			synchroService.savePubDditem();
		logger.info("service同步数据字典结束，耗时：[{}]秒",s1.elapsedTime()/1000);
	}
	
	
	@RequestMapping(value = "/o2o/scheduler/service/synchroAfterLoanForCustomer")
	public void syncForService() {
		StopWatch s1=new StopWatch();
		logger.info("service按客户维度同步贷款信息开始");
		 synchroAfterLoanForService.synchroAfterLoanForCustomer();
		logger.info("service按客户维度同步贷款信息结束，耗时：[{}]秒",s1.elapsedTime()/1000);
	}

	@RequestMapping(value = "/o2o/scheduler/service/synchroAfterLoanForBillCode")
	public void syncForService1() {
		StopWatch s=new StopWatch();
		logger.info("service按贷款单维度同步贷款信息开始");
			synchroAfterLoanForService.synchroAfterLoanForBillCode();
		logger.info("service按贷款单维度同步贷款信息结束，耗时：[{}]秒",s.elapsedTime()/1000);
	}
	
	@RequestMapping(value = "/o2o/scheduler/service/synchBalanceHis")
	public void synchBalanceHisTest() {
		StopWatch s=new StopWatch();
		logger.info("service同步放款余额历史存档开始");
		synchroAfterLoanForService.synchBalanceHis();
		logger.info("service同步放款余额历史存档结束，耗时：[{}]秒",s.elapsedTime()/1000);
	}
	
	@RequestMapping(value = "/o2o/scheduler/service/synchReLoan")
	public void synchReLoan() {
		StopWatch s=new StopWatch();
		logger.info("service同步续贷状态开始");
		synchroAfterLoanForService.synchReLoan();
		logger.info("service同步续贷状态结束，耗时：[{}]秒",s.elapsedTime()/1000);
	}
	
	@RequestMapping(value = "/o2o/scheduler/service/updateCreditStatus")
	public void updateCreditStatus() {
		StopWatch s=new StopWatch();
		logger.info("service同步征信状态开始");
			synchroService.updateCreditStatus();
		logger.info("service同步征信状态结束，耗时：[{}]秒",s.elapsedTime()/1000);
	}
	
	@RequestMapping(value = "/o2o/scheduler/service/loadWToS")
	public void loadWToS() {
		StopWatch s=new StopWatch();
		logger.info("service同步省区和信贷经理关系开始");
			synchroService.loadWToS();
		logger.info("service同步省区和信贷经理关系结束，耗时：[{}]秒",s.elapsedTime()/1000);
	}
	
	@RequestMapping(value = "/o2o/scheduler/service/saveO2oPlatformRegional")
	public void saveO2oPlatformRegional() {
		StopWatch s=new StopWatch();
		logger.info("service同步大区和平台关系开始");
			adminSynchroTask.saveO2oPlatformRegional();
		logger.info("service同步大区和平台关系结束，耗时：[{}]秒",s.elapsedTime()/1000);
	}
	
	
	@RequestMapping(value = "/o2o/scheduler/service/syncBalance")
	public void syncBalance() {
		StopWatch s=new StopWatch();
		logger.info("service更新上年年末代偿数据开始");
		//synchroAfterLoanForService.updateOverdue();//逾期数据直接业务给
		synchroAfterLoanForService.updateCompensatory();
		logger.info("service更新上年年末代偿数据结束，耗时：[{}]秒",s.elapsedTime()/1000);
	}
	
	@RequestMapping(value = "/o2o/scheduler/service/archive")
	public void archive() {
		adminSynchroTask.archive();
	}
	
	/**
	 * 保存资产结构本金日报表数据
	 */
	@RequestMapping(value = "/o2o/scheduler/saveAssetStructurePrincipalDaily")
	public void saveAssetStructurePrincipalDaily() {
		try {
			synoInfoAssetStructureBjService.saveAssetStructurePrincipalDaily();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 同步资产数椐
	 */
	@RequestMapping(value = "/o2o/scheduler/saveCrmByServiceAsset")
	public void CrmByServiceAsset() {
		try {
			StopWatch stopWatch = new StopWatch();
			//crmByServiceService.CrmByService();
			logger.info("同步资产数椐任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @Title: sychRemouldCustomer
	 * @Description: 从crm 同步客户基本信息到service表中
	 * @author 张庆奇
	 * @return void
	 * @throws
	 * @date 2017年7月11日 下午4:34:16
	 */
	@RequestMapping(value = "/o2o/scheduler/sychRemouldCustomer")
	public void sychRemouldCustomer() {
		try {
			StopWatch stopWatch = new StopWatch();
			logger.info("同步客户扩展信息数据任务开始......");
			//synchroService.sychRemouldCustomer();
			logger.info("同步客户扩展信息数据任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * @Title: sych_eaba_customer_new
	 * @Description: 同步品牌贷sd数据
	 * @author 张庆奇
	 * @return void
	 * @throws
	 * @date 2017年7月14日 上午9:49:20
	 */
	@RequestMapping(value = "/o2o/scheduler/sych_eaba_customer_new")
	public void sych_eaba_customer_new() {
		try {
			StopWatch stopWatch = new StopWatch();
			logger.info("同步品牌贷sd数据任务开始......");
			//synchroService.sych_eaba_customer_new();
			logger.info("同步品牌贷sd数据任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @Title: sych_customer_shop_info
	 * @Description: 同步门店信息数据
	 * @author 张庆奇
	 * @return void
	 * @throws
	 * @date 2017年7月14日 上午10:26:42
	 */
	@RequestMapping(value = "/o2o/scheduler/sych_customer_shop_info")
	public void sych_customer_shop_info() {
		try {
			StopWatch stopWatch = new StopWatch();
			logger.info("同步门店信息数据任务开始......");
			//crmByServiceService.sych_customer_shop_info();
			logger.info("同步门店信息数据任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping(value = "/o2o/scheduler/saveO2oPlatformRegional2")
	public void saveO2oPlatformRegional2() {
		try {
			synchroService.saveO2oPlatformRegional2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @Title: cancelLoanapply
	 * @Description: 贷款申请超时作废
	 * @author 张庆奇
	 * @return void
	 * @throws
	 * @date 2017年8月18日 上午9:47:28
	 */
	@RequestMapping(value = "/o2o/scheduler/cancelLoanapply")
	public void cancelLoanapply() {
		try {
			synchroService.cancelLoanapply();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新建状态自动作废按钮
	 * 
	 * @return
	 */
	@RequestMapping("/o2o/scheduler/loanNewCancle")
	public BaseRestResult<String> loanNewCancle() {
		com.eascs.admin.common.vo.ModelResult<Boolean> r = iCancelService.loanNewCancle();
		if (r.isSuccess()) {
			return BaseRestResult.info("作废成功");
		} else {
			logger.info("作废失败, message:[{}]", r.getErrMsg());
			return BaseRestResult.warn("作废失败,message:"+r.getErrMsg());
		}
	}

	/**
	 * @Title: createPersonalAccount
	 * @Description:创建个人账户
	 * @author 张庆奇
	 * @return void
	 * @throws
	 * @date 2017年11月28日 下午2:51:29
	 */
	@RequestMapping("/o2o/scheduler/createPersonalAccount")
	public void createPersonalAccount() {
		StopWatch stopWatch = new StopWatch();
		logger.info("创建个人账户任务开始......");
		personalAccountService.createPersonalAccount();
		logger.info("创建个人账户任务任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
	}
	
	
	
	
	
	@RequestMapping("/o2o/scheduler/LoanAccountBZJHK")
	public void LoanAccountBZJHK() {
		StopWatch stopWatch = new StopWatch();
		logger.info("创建单款单账户任务开始......");
		o2oLoanapplyAccountService.LoanAccountBZJHK();
		logger.info("创建贷款单账户任务任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
	}
	/**
	 * loan-13099 单据驳回5个工作日内自动作废
	 */
	@RequestMapping("/o2o/scheduler/cancelBHLoanapply")
	public void cancelBHLoanapply(){
		logger.info("[service] [贷款驳回超时5工作日作废开始]任务[开始]......");
		StopWatch time = new StopWatch();
		try {
			synchroService.cancelBHLoanapply();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[service] [贷款驳回超时5工作日作废]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}
	
}